// pages/testtitle/index.js
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		titles: [], //题目
		idx: 0, //当前题目
		results: [], //结果记录
		scores: [1, 2, 3, 4], //分数
		studentId: 0, //学生id
		complete: false, //是否完成测评
		questionTypeId: 1 //题型
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		if (options.studentId) {
			this.setData({
				studentId: options.studentId,
				questionTypeId: options.questionTypeId
			})
		};
		var that = this;
		var loginInfo = loginFn()
		this.getquestion(loginInfo, options.questionTypeId) //获取题目
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.setData({
			idx: 0,
			results: [],
			complete: false
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	answerFn: function(e) { //回答问题
		console.log(e)
		if (this.data.idx < this.data.titles.length - 1) {
			var hasresults = true;
			var results = this.data.results;
			for (var i = 0; i < results.length; i++) {
				if (results[i].questionDimensionId == this.data.titles[this.data.idx].questionDimensionId) {
					hasresults = false
					results[i].score = results[i].score + this.data.scores[e.currentTarget.dataset.idx]
					results[i].num = results[i].num + 1
					break
				}
			}
			if (hasresults) { //判断有没有记录改结果
				results.push({
					"questionDimensionId": this.data.titles[this.data.idx].questionDimensionId,
					"score": this.data.scores[e.currentTarget.dataset.idx],
					"dimension": this.data.titles[this.data.idx].dimension,
					"num": 1
				})
			};
			console.log(results)
			this.setData({
				idx: this.data.idx + 1,
				results: results
			})

		} else {
			if (!this.data.complete) {
				this.setData({
					complete: true
				})
				wx.showToast({
					title: '测试完成',
				})
				var results = this.data.results
				for (var i = 0; i < results.length; i++) {
					results[i].score = 100 * results[i].score / (results[i].num * 4)
				}
				console.log(results)
				var loginInfo = loginFn()
				loginInfo.answer = results
				loginInfo.studentId = this.data.studentId
				var that = this
				wx.request({
					url: urls.questionScore,
					data: loginInfo,
					method: 'POST',
					success: function(res) {
						if (res.data.code == '0') {
							wx.navigateTo({
								url: '/pages/results/index?studentId=' + that.data.studentId + '&questionTypeId=' + that.data.questionTypeId,
							})
						};
					}
				})
			} else {
				wx.showToast({
					title: '测试完成',
				})
			};

		}

	},
	getquestion: function(loginInfo, questionTypeId) { //获取所有问题
		var that = this;
		loginInfo.questionTypeId = questionTypeId
		wx.request({
			url: urls.questionUrl,
			data: loginInfo,
			method: 'POST',
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					that.setData({
						titles: res.data.data
					})
				};
			}
		})
	}
})
